library(tidyverse)
library(kableExtra)
library(sf)
library(ggspatial)

1 Loading the Map Data Files

proj_dir = "/Volumes/GDRIVE_SSD/homes/alex/datascience/698_CAPSTONE_2021_FALL"

working_dir = paste0(proj_dir, "/", "project_code/Part2_GeoMapping/")

data_dir = paste0(proj_dir, "/project_data/")

setwd(working_dir)
raleigh_corporate_limits_file = paste0(data_dir, "Corporate_Limits.geojson")

cityworks_potholes_file = paste0(data_dir, "Cityworks_Potholes.geojson" )

raleigh_parks_file = paste0(data_dir, "Raleigh_Parks.geojson")
gj_potholes = sf::st_read(cityworks_potholes_file, quiet = TRUE ) %>% st_transform(2264)
gj_parks = sf::st_read(raleigh_parks_file, quiet = TRUE ) %>% st_transform(2264)
#
# Load the geojson files
# change the coordinate system to 2264
# filter objects that belong to RALEIGHT
# --------------------------------------------
gj_raleigh <- sf::st_read(raleigh_corporate_limits_file, quiet = TRUE) %>% 
  st_transform(2264) %>% 
  filter( SHORT_NAME == 'RAL' )

head(gj_raleigh) %>% select(-ORDINANCE_NUMBER, -EFFECTIVE_DATE, -SHAPEAREA ) %>% st_drop_geometry() %>%
  kable() %>% kable_styling(bootstrap_options = c("striped", "hover"))
OBJECTID SHORT_NAME LONG_NAME SHAPELEN
3451895 RAL RALEIGH 6316.925
3452243 RAL RALEIGH 2973.118
3452244 RAL RALEIGH 10043.695
3452245 RAL RALEIGH 1351.472
3452246 RAL RALEIGH 9113.943
3452247 RAL RALEIGH 9077.708

We compute the area of the polygonal representation and validate by checking against Wikipedia’s reported area. They are very close.

# Compute the total square miles of the polygons.
# The NAD83 North Carolina projection uses square ft.
# ------------------------------------------------------
area_of_polygons = st_area(gj_raleigh)

sprintf( "Raleigh Area is: %.2f square miles.  Wikipedia gives: 147.64 sq mi" , sum(area_of_polygons) / ( 5280^2)  )
## [1] "Raleigh Area is: 149.06 square miles.  Wikipedia gives: 147.64 sq mi"

The square mile area for most areas look accurate. However, note that they show area inside Wake County. Thus, Durham has less than 1 mile inside Wake County but actual area is over 100 squared miles.

a = sf::st_read(raleigh_corporate_limits_file, quiet = TRUE) %>%   st_transform(2264) 

a = a %>% mutate( my_area = st_area(a)) %>% mutate( sqmiles = my_area/ (5280^2)) %>% st_drop_geometry() %>%
   select(SHORT_NAME, LONG_NAME, sqmiles) %>% group_by(SHORT_NAME, LONG_NAME) %>% summarize( sqmiles = sum( sqmiles)) 
## `summarise()` has grouped output by 'SHORT_NAME'. You can override using the `.groups` argument.
attributes( a$sqmiles ) = NULL

a %>% kable(digits = 2, caption = "Square Miles of Towns inside Wake County")
Square Miles of Towns inside Wake County
SHORT_NAME LONG_NAME sqmiles
ANG ANGIER 0.62
APE APEX 24.82
CAR CARY 59.04
CLA CLAYTON 0.09
DUR DURHAM 0.26
FUQ FUQUAY-VARINA 18.44
GAR GARNER 17.64
HOL HOLLY SPRINGS 18.20
KNI KNIGHTDALE 8.20
MOR MORRISVILLE 8.86
RAL RALEIGH 149.06
ROL ROLESVILLE 6.15
WAK WAKE FOREST 19.09
WEN WENDELL 6.47
ZEB ZEBULON 5.65